package cst.a931xmc.yuejian20.Activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import cst.a931xmc.yuejian20.Bean.AppBean;
import cst.a931xmc.yuejian20.Bean.TrainBean;
import cst.a931xmc.yuejian20.Listener.Listener;
import cst.a931xmc.yuejian20.Model.ApiModel;
import cst.a931xmc.yuejian20.Model.TrainModel;
import cst.a931xmc.yuejian20.R;

public class TrainAppActivity extends AppCompatActivity implements View.OnClickListener {
    private Context context;
    private int train_id;
    private int user_id;
    private int train_appnum;
    private ImageView train_pic;
    private TextView train_name;
    private TextView train_price;
    private TextView train_appnotice;
    private EditText torder_rname;
    private EditText torder_tel;
    private EditText torder_idcard;
    private EditText torder_num;
    private EditText torder_price;
    private ImageButton btn_back;
    private ImageButton btn_off;
    private ImageButton btn_add;
    private Button btn_pay;
    private TrainModel model;
    private Listener<AppBean> listener;
    private Listener<TrainBean> listener1;
    private int per;
    private String trainname;
    private String trainpic;
    private String trainintro;
    SharedPreferences sp;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trainapp);
        init();
        initData();
        model = new TrainModel();
        model.getDetail(train_id,listener1);
    }

    public void init(){
        train_pic = findViewById(R.id.train_pic);
        train_name = findViewById(R.id.train_name);
        train_price = findViewById(R.id.train_price);
        train_appnotice = findViewById(R.id.train_appnotice);
        torder_rname = findViewById(R.id.torder_rname);
        torder_tel = findViewById(R.id.torder_tel);
        torder_idcard = findViewById(R.id.torder_idcard);
        torder_num = findViewById(R.id.torder_num);
        torder_price = findViewById(R.id.torder_price);
        btn_back = findViewById(R.id.btn_back);
        btn_off = findViewById(R.id.btn_off);
        btn_add = findViewById(R.id.btn_add);
        btn_pay = findViewById(R.id.btn_pay);
        btn_back.setOnClickListener(this);
        btn_off.setOnClickListener(this);
        btn_add.setOnClickListener(this);
        btn_pay.setOnClickListener(this);
        sp = getSharedPreferences("user_id", Context.MODE_PRIVATE);
        Intent intent = getIntent();
        train_id = intent.getExtras().getInt("train_id");
        user_id = sp.getInt("user_id",0);
    }

    public void initData(){
        listener1 = new Listener<TrainBean>() {
            @Override
            public void onResponse(TrainBean l) {
                if(l.getTrain_id()!=0){
                    train_name.setText(l.getTrain_name());
                    train_price.setText("￥" + l.getTrain_price() + "/人次");
                    train_appnotice.setText(l.getTrain_appnotice());
                    train_appnum = Integer.parseInt(l.getTrain_appnum());
                    trainname = l.getTrain_name();
                    trainpic = l.getTrain_pic();
                    trainintro = l.getTrain_intro();
                    per = Integer.parseInt(l.getTrain_price());
                    torder_price.setText(l.getTrain_price());
                    ApiModel apiModel = new ApiModel();
                    Picasso.with(context)
                            .load(apiModel.getBaseUrl() + "static/images/" + l.getTrain_pic())
                            .placeholder(R.drawable.nopic)
                            .into(train_pic);
                }
                else {
                    Toast.makeText(TrainAppActivity.this,"加载失败",Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFail(String message) {
                Toast.makeText(TrainAppActivity.this,message, Toast.LENGTH_LONG).show();
            }
        };

        listener = new Listener<AppBean>() {
            @Override
            public void onResponse(AppBean l) {
                if(l.getOrder_id()!=0){
                    Intent intent = new Intent(TrainAppActivity.this,SuccessActivity.class);
                    intent.putExtra("torder_id",l.getOrder_id());
                    startActivity(intent);
                    finish();
                    Toast.makeText(TrainAppActivity.this, "报名成功", Toast.LENGTH_LONG).show();
                }
                else {
                    Toast.makeText(TrainAppActivity.this, "报名失败", Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFail(String message) {
                Toast.makeText(TrainAppActivity.this,message, Toast.LENGTH_LONG).show();
            }
        };
    }

    @Override
    public void onClick(View v) {
        int num = Integer.parseInt(torder_num.getText().toString());
        switch (v.getId()){
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_off:
                if(num!=1){
                    torder_num.setText(Integer.toString(--num));
                }
                else {
                    Toast.makeText(TrainAppActivity.this, "报名数量至少为1", Toast.LENGTH_LONG).show();
                }
                torder_price.setText(Integer.toString(per*num));
                break;
            case R.id.btn_add:
                if(num!=10){
                    torder_num.setText(Integer.toString(++num));
                }
                else {
                    Toast.makeText(TrainAppActivity.this, "报名数量上限为10", Toast.LENGTH_LONG).show();
                }
                torder_price.setText(Integer.toString(per*num));
                break;
            case R.id.btn_pay:

                if(TextUtils.isEmpty(torder_rname.getText().toString())){
                    Toast.makeText(TrainAppActivity.this, "姓名不能为空", Toast.LENGTH_LONG).show();
                }
                else if(TextUtils.isEmpty(torder_tel.getText().toString())){
                    Toast.makeText(TrainAppActivity.this, "电话不能为空", Toast.LENGTH_LONG).show();
                }
                else if(TextUtils.isEmpty(torder_idcard.getText().toString())){
                    Toast.makeText(TrainAppActivity.this, "身份证号不能为空", Toast.LENGTH_LONG).show();
                }
                else {
                    model = new TrainModel();
                    model.order(train_id,trainname,trainpic,trainintro,user_id,
                            torder_rname.getText().toString(),
                            torder_tel.getText().toString(),
                            torder_idcard.getText().toString(),
                            Integer.parseInt(torder_num.getText().toString()),
                            torder_price.getText().toString(),
                            listener);
                    train_appnum += Integer.parseInt(torder_num.getText().toString());
                    model.updateTrainAppNum(train_id,train_appnum);
                }
        }
    }
}
